package com.ymm.lulutong.controller;

import com.ymm.lulutong.constant.ResultCodeEnum;
import com.ymm.lulutong.domain.ShipperProfileDomainService;
import com.ymm.lulutong.entity.Shipper;
import com.ymm.lulutong.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/user")
public class ShipperUserController {
    @Resource
    private ShipperProfileDomainService shipperProfileDomainService;

    /**
     * 登录
     *
     * @param phone    手机号
     * @param password 密码
     * @return 货主实体
     */
    @PostMapping("/login")
    public Result<Shipper> login(String phone, String password) {
        if (password == null || phone == null) {
            log.info("登录失败！传入参数错误！" + password + phone);
            return Result.fail(null, ResultCodeEnum.PARAMETER_NULL_ERROR.getCode(), ResultCodeEnum.PARAMETER_NULL_ERROR.getMessage());
        }
        return shipperProfileDomainService.login(phone, password);
    }

    /**
     * 注册
     *
     * @param phone            手机号
     * @param password         密码
     * @param passwordValidate 确认密码
     * @param shipperNickname  昵称
     * @return 货主实体
     */
    @PostMapping("/register")
    public Result<Shipper> register(String phone, String password, String passwordValidate, String shipperNickname) {
        if (phone == null || password == null || passwordValidate == null || shipperNickname == null) {
            log.info("注册失败！传入参数错误！");
            return Result.fail(null, ResultCodeEnum.PARAMETER_NULL_ERROR.getCode(), ResultCodeEnum.PARAMETER_NULL_ERROR.getMessage());
        }
        return shipperProfileDomainService.register(phone, password, passwordValidate, shipperNickname);
    }

    /**
     * 更新密码
     *
     * @param shipperId           货主Id
     * @param newPassword         新的密码
     * @param newPasswordValidate 确认密码
     * @return 货主实体
     */
    @PostMapping("/update/password")
    public Result<Shipper> updatePassword(Long shipperId, String newPassword, String newPasswordValidate) {
        if (shipperId == null || newPassword == null || newPasswordValidate == null) {
            log.info("修改密码失败！传入参数错误！");
            return Result.fail(null, ResultCodeEnum.PARAMETER_NULL_ERROR.getCode(), ResultCodeEnum.PARAMETER_NULL_ERROR.getMessage());
        }
        return shipperProfileDomainService.updatePassword(shipperId, newPassword, newPasswordValidate);
    }

    /**
     * 找回密码
     *
     * @param phoneNumber         手机号
     * @param newPassword         新的密码
     * @param newPasswordValidate 确认密码
     * @return 货主实体
     */
    @PostMapping("/retrievePassword")
    public Result<Shipper> retrievePassword(String phoneNumber, String newPassword, String newPasswordValidate) {
        if (phoneNumber == null || newPassword == null || newPasswordValidate == null) {
            log.info("找回密码失败！传入参数错误！");
            return Result.fail(null, ResultCodeEnum.PARAMETER_NULL_ERROR.getCode(), ResultCodeEnum.PARAMETER_NULL_ERROR.getMessage());
        }
        return shipperProfileDomainService.retrievePassword(phoneNumber, newPassword, newPasswordValidate);
    }

    /**
     * 更新手机号
     *
     * @param shipperId 货主Id
     * @param newPhone  新的手机号
     * @return 货主实体
     */
    @PostMapping("/update/phoneNumber")
    public Result<Shipper> updatePhoneNumber(Long shipperId, String newPhone) {
        if (shipperId == null || newPhone == null) {
            log.info("修改手机号失败！传入参数错误！");
            return Result.fail(null, ResultCodeEnum.PARAMETER_NULL_ERROR.getCode(), ResultCodeEnum.PARAMETER_NULL_ERROR.getMessage());
        }
        return shipperProfileDomainService.updatePhoneNumber(shipperId, newPhone);
    }

    /**
     * @param shipperId 货主Id
     * @return 货主实体
     */
    @GetMapping("/queryShipperInfo")
    public Result<Shipper> getShipperByShipperId(Long shipperId) {
        if (shipperId == null) {
            log.info("传入参数错误！");
            return Result.fail(null, ResultCodeEnum.PARAMETER_NULL_ERROR.getCode(), ResultCodeEnum.PARAMETER_NULL_ERROR.getMessage());
        }
        return shipperProfileDomainService.getShipperByShipperId(shipperId);
    }

    /**
     * 更新头像
     *
     * @param shipperId 货主Id
     * @param image     头像图片
     * @return 货主实体
     */
    @PostMapping("/update/profile_picture")
    public Result<Shipper> updateProfilePicture(Long shipperId, MultipartFile image) {
        //判断图片是否为空
        if (shipperId == null || image == null) {
            return Result.fail(null, ResultCodeEnum.PARAMETER_NULL_ERROR.getCode(), "入参错误");
        }
        return shipperProfileDomainService.updateProfilePicture(shipperId, image);
    }


}
